package cn.lazyturtle.bugpush.config;

import org.jooq.Configuration;
import org.jooq.DSLContext;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import org.jooq.impl.DataSourceConnectionProvider;
import org.jooq.impl.DefaultConfiguration;
import org.jooq.impl.DefaultExecuteListenerProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;

import javax.sql.DataSource;


/**
 * @author Administrator
 */
public class JOOQConfigration {


    @Bean
    public JooqToSpringExceptionTransformer jooqToSpringExceptionTransformer() {
        return new JooqToSpringExceptionTransformer();
    }

    Configuration getConfiguration(DataSource dataSource) {
        // 配置JOOQ
        TransactionAwareDataSourceProxy proxy = new TransactionAwareDataSourceProxy(dataSource);
        return new DefaultConfiguration().set(new DataSourceConnectionProvider(proxy))
                .set(SQLDialect.MYSQL)
                .set(new DefaultExecuteListenerProvider(jooqToSpringExceptionTransformer()));
    }

    DSLContext getDslContext(Configuration configuration) {
        // 领域对象编程
        return DSL.using(configuration);
    }
}
